/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Entity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.LinkedList;
import java.util.StringTokenizer;

/**
 *
 * @author Ardha
 */
public class RekamMedis {

    private int no_Rekam_Medis;
    private String id_Pasien;
    private String kode_kategori;
    private String kode_Penyakit;
    private String tipe_Pasien;
    private String singleGejala;
    private String tanggal_Periksa;
    private String[] gejala;
    private String no_Reg_Labvarchar;
    private String tanggal_Periksa_Lab;
    private int jumlah_bta;
    private int hasil_Rontgent;
    private float berat_Badan;
    private int sisa_Obat;
    private String nama_PMO;
    private String alamat_PMO;
    private Connection konek = null;
    private static RekamMedis koneksi;
    private String tahap;

    public RekamMedis() {
    }

    public RekamMedis(Connection konek) {
        this.konek = konek;
    }

    public static RekamMedis getKoneksiKontrol()
            throws SQLException, FileNotFoundException, IOException {
        if (koneksi == null) {
            koneksi = new RekamMedis(Koneksi.getDBConnection());
        }
        return koneksi;
    }

    /**
     * @return the no_Rekam_Medis
     */
    public int getNo_Rekam_Medis() {
        return no_Rekam_Medis;
    }

    /**
     * @param no_Rekam_Medis the no_Rekam_Medis to set
     */
    public void setNo_Rekam_Medis(int no_Rekam_Medis) {
        this.no_Rekam_Medis = no_Rekam_Medis;
    }

    /**
     * @return the id_Pasien
     */
    public String getId_Pasien() {
        return id_Pasien;
    }

    /**
     * @param id_Pasien the id_Pasien to set
     */
    public void setId_Pasien(String id_Pasien) {
        this.id_Pasien = id_Pasien;
    }

    /**
     * @return the kode_kategori
     */
    public String getKode_Kategori() {
        return kode_kategori;
    }

    /**
     * @param kode_kategori the kode_kategori to set
     */
    public void setKode_Kategori(String kode_kategori) {
        this.kode_kategori = kode_kategori;
    }

    /**
     * @return the kode_Penyakit
     */
    public String getKode_Penyakit() {
        return kode_Penyakit;
    }

    /**
     * @param kode_Penyakit the kode_Penyakit to set
     */
    public void setKode_Penyakit(String kode_Penyakit) {
        this.kode_Penyakit = kode_Penyakit;
    }

    /**
     * @return the tipe_Pasien
     */
    public String getTipe_Pasien() {
        return tipe_Pasien;
    }

    /**
     * @param tipe_Pasien the tipe_Pasien to set
     */
    public void setTipe_Pasien(String tipe_Pasien) {
        this.tipe_Pasien = tipe_Pasien;
    }

    /**
     * @return the tanggal_Periksa
     */
    public String getTanggal_Periksa() {
        return tanggal_Periksa;
    }

    /**
     * @param tanggal_Periksa the tanggal_Periksa to set
     */
    public void setTanggal_Periksa(String tanggal_Periksa) {
        this.tanggal_Periksa = tanggal_Periksa;
    }

    /**
     * @return the gejala
     */
    public String[] getGejala() {
        return gejala;
    }

    /**
     * @param gejala the gejala to set
     */
    public void setGejala(String[] gejala) {
        this.gejala = gejala;
    }

    /**
     * @return the singleGejala
     */
    public String getSingleGejala() {
        return singleGejala;
    }

    /**
     * @param singleGejala the singleGejala to set
     */
    public void setSingleGejala(String singleGejala) {
        this.singleGejala = singleGejala;
    }

    /**
     * @return the no_Reg_Labvarchar
     */
    public String getNo_Reg_Labvarchar() {
        return no_Reg_Labvarchar;
    }

    /**
     * @param no_Reg_Labvarchar the no_Reg_Labvarchar to set
     */
    public void setNo_Reg_Labvarchar(String no_Reg_Labvarchar) {
        this.no_Reg_Labvarchar = no_Reg_Labvarchar;
    }

    /**
     * @return the tanggal_Periksa_Lab
     */
    public String getTanggal_Periksa_Lab() {
        return tanggal_Periksa_Lab;
    }

    /**
     * @param tanggal_Periksa_Lab the tanggal_Periksa_Lab to set
     */
    public void setTanggal_Periksa_Lab(String tanggal_Periksa_Lab) {
        this.tanggal_Periksa_Lab = tanggal_Periksa_Lab;
    }

    /**
     * @return the jumlah_bta
     */
    public int getJumlah_bta() {
        return jumlah_bta;
    }

    /**
     * @param jumlah_bta the jumlah_bta to set
     */
    public void setJumlah_bta(int jumlah_bta) {
        this.jumlah_bta = jumlah_bta;
    }

    /**
     * @return the hasil_Rontgent
     */
    public int getHasil_Rontgent() {
        return hasil_Rontgent;
    }

    /**
     * @param hasil_Rontgent the hasil_Rontgent to set
     */
    public void setHasil_Rontgent(int hasil_Rontgent) {
        this.hasil_Rontgent = hasil_Rontgent;
    }

    /**
     * @return the berat_Badan
     */
    public float getBerat_Badan() {
        return berat_Badan;
    }

    /**
     * @param berat_Badan the berat_Badan to set
     */
    public void setBerat_Badan(float berat_Badan) {
        this.berat_Badan = berat_Badan;
    }

    /**
     * @return the sisa_Obat
     */
    public int getSisa_Obat() {
        return sisa_Obat;
    }

    /**
     * @param sisa_Obat the sisa_Obat to set
     */
    public void setSisa_Obat(int sisa_Obat) {
        this.sisa_Obat = sisa_Obat;
    }

    /**
     * @return the nama_PMO
     */
    public String getNama_PMO() {
        return nama_PMO;
    }

    /**
     * @param nama_PMO the nama_PMO to set
     */
    public void setNama_PMO(String nama_PMO) {
        this.nama_PMO = nama_PMO;
    }

    /**
     * @return the alamat_PMO
     */
    public String getAlamat_PMO() {
        return alamat_PMO;
    }

    /**
     * @param alamat_PMO the alamat_PMO to set
     */
    public void setAlamat_PMO(String alamat_PMO) {
        this.alamat_PMO = alamat_PMO;
    }

    public LinkedList getGejala(Pasien pasien) throws SQLException, FileNotFoundException, IOException {
        Statement stmt = konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String query = "select jumlah_bta, Hasil_Rontgen, gejala from rekam_medis where Id_Pasien='" + pasien.getId()
                + "' and No_Rekam_Medis=(select max(No_Rekam_Medis) "
                + "from rekam_medis where upper(Id_Pasien)='" + pasien.getId().toUpperCase() + "')";
        ResultSet rset = stmt.executeQuery(query);
        String gejalaPasien = null;
        int bta = 0;
        int rongent = 0;
        if (rset.first()) {
            bta = rset.getInt(1);
            rongent = rset.getInt(2);
            gejalaPasien = rset.getString(3);
            if (gejalaPasien == null) {
                gejalaPasien = "";
            }
            System.out.println(gejalaPasien);
        }
        LinkedList list = new LinkedList();
        if (!gejalaPasien.matches("") || !gejalaPasien.isEmpty() || gejalaPasien != null) {
            StringTokenizer tokens = new StringTokenizer(gejalaPasien, ",");
            String result[] = new String[tokens.countTokens()];

            for (int i = 0; i < result.length; i++) {
                String gjl = tokens.nextToken();
                if (gjl.startsWith(" ")) {
                    result[i] = gjl.substring(1);
                } else {
                    result[i] = gjl;
                }
            }

            list.add(bta);
            list.add(rongent);
            list.add(result);
        } else {
            list.add(bta);
            list.add(rongent);
            list.add(null);
        }
        return list;
    }

    public LinkedList getDataRekamMedis(Pasien pasien) throws SQLException, FileNotFoundException, IOException {
        Statement stmt = konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String query = "SELECT Kode_Kategori,Tanggal_Periksa,Tipe_Pasien, No_Reg_Lab,"
                + "Tanggal_Periksa_Lab, jumlah_bta, Hasil_Rontgen, Berat_Badan, Sisa_Obat "
                + "from rekam_medis where upper(Id_Pasien)='" + pasien.getId().toUpperCase() + "'";
        ResultSet rset = stmt.executeQuery(query);
        LinkedList list = new LinkedList();
        while (rset.next()) {
            list.add(rset.getString(1));
            list.add(rset.getDate(2));
            list.add(rset.getString(3));
            list.add(rset.getString(4));
            list.add(rset.getDate(5));
            list.add(rset.getInt(6));
            list.add(rset.getInt(7));
            list.add(rset.getFloat(8));
            list.add(rset.getInt(9));
        }
        return list;
    }

    /**
     * @return the tahap
     */
    public String getTahap() {
        return tahap;
    }

    /**
     * @param tahap the tahap to set
     */
    public void setTahap(String tahap) {
        this.tahap = tahap;
    }

    public String getKategoriRekamMedis(String id_Pasien) throws SQLException{
       String temp = null;
       String query = "SELECT Kode_Kategori FROM rekam_medis "
               + "WHERE UPPER(Id_Pasien)=(?) "
               + "AND No_Rekam_Medis = (SELECT MAX(No_Rekam_Medis) "
               + "FROM rekam_medis WHERE Id_Pasien = (?))";
       PreparedStatement psmt = konek.prepareStatement(query);
        psmt.setObject(1, id_Pasien);
        psmt.setObject(2, id_Pasien);
        ResultSet rset=psmt.executeQuery();
        if (rset.first()) {
            temp = rset.getString(1);
        }
       return temp;
    }

    public void insertRekamMedis(int no_Rekam_Medis, String id_Pasien, String kode_kategori,
            String kode_Penyakit, String tipe_Pasien, String tanggal_Periksa, String gejala, String no_Reg_Labvarchar,
            String tanggal_Periksa_Lab, int jumlah_bta, int hasil_Rontgent, float berat_Badan,
            int sisa_Obat, String nama_PMO, String alamat_PMO, String tahap_Priksa) throws SQLException {
//        Statement stmt = konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String query = "INSERT INTO rekam_medis VALUES ((select max(rm.No_Rekam_Medis)+1 from rekam_medis rm), (?)"
                + ", (?),(?),(?),(?),(?)"
                + ",(?),(?),(?),(?),(?),(?)"
                + ",(?),(?),(?))";
        System.out.println("["+id_Pasien+"]");
        PreparedStatement psmt = konek.prepareStatement(query);
        psmt.setObject(1, id_Pasien);
        System.out.println("NOMER GOLONGAN: "+kode_kategori);
        if(!kode_kategori.matches("K0"))
            psmt.setObject(2, kode_kategori);
        else
            psmt.setObject(2, "K0");

        if (kode_Penyakit!=null)
            psmt.setObject(3, kode_Penyakit);
        else
            psmt.setObject(3, null);
        
        psmt.setObject(4, tipe_Pasien);
        psmt.setObject(5, tanggal_Periksa);
        psmt.setObject(6, gejala);
        psmt.setObject(7, no_Reg_Labvarchar);
        psmt.setObject(8, tanggal_Periksa_Lab);
        psmt.setObject(9, jumlah_bta);
        psmt.setObject(10, hasil_Rontgent);
        psmt.setObject(11, berat_Badan);
        psmt.setObject(12, sisa_Obat);
        psmt.setObject(13, nama_PMO);
        psmt.setObject(14, alamat_PMO);
        psmt.setObject(15, tahap_Priksa);
        psmt.executeUpdate();
//        String query = "INSERT INTO rekam_medis VALUES ((select max(rm.No_Rekam_Medis)+1 from rekam_medis rm), '" + id_Pasien
//                + "', null, null," + tipe_Pasien + "," + tanggal_Periksa + "," + gejala
//                + "," + no_Reg_Labvarchar + "," + tanggal_Periksa + "," + jumlah_bta + "," + hasil_Rontgent + "," + berat_Badan + "," + sisa_Obat
//                + "," + nama_PMO + "," + alamat_PMO + "," + tahap_Priksa + ")";
//        stmt.executeUpdate(query);
//        stmt.close();
    }
//    public int getJumlahBta(Pasien pasien) throws SQLException{
//        Statement stmt = konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//        String query = "SELECT jumlah_bta from rekam_medis where upper(Id_Pasien)='" + pasien.getId().toUpperCase()
//                + "'and No_Rekam_Medis=(select max(No_Rekam_Medis) "
//                + "from rekam_medis where upper(Id_Pasien)='" + pasien.getId().toUpperCase() + "')";
//        ResultSet rset = stmt.executeQuery(query);
//        int jumlahBTA=0;
//        while (rset.next()) {
//            jumlahBTA=rset.getInt(1);
//        }
//        return jumlahBTA;
//    }
}
